//
//  HeadView.m
//  CarTown
//
//  Created by 熊浩 on 15/8/1.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "HeadView.h"
#import "ScrollViewItem.h"
@interface HeadView()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scollview;
@property (weak, nonatomic) IBOutlet UIPageControl *pagecontrol;
@property (nonatomic,strong) ScrollViewItem *item;
@property (nonatomic,strong) NSTimer *timer;


@end

@implementation HeadView

+(instancetype)header{
    HeadView *header=[[[NSBundle mainBundle] loadNibNamed:@"HeadView" owner:nil options:nil]lastObject];
    return header;
}

-(void)awakeFromNib{
    self.timer= [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(pagechange) userInfo:nil repeats:YES];
    self.width=XHScreenwidth;
}
-(void)pagechange{
    NSInteger page=self.pagecontrol.currentPage;
    page++;
    if (page==self.picarray.count) {
        page=0;
    }
    [self.scollview setContentOffset:CGPointMake(page*XHScreenwidth, 0) animated:YES];
    
}

-(void)dealloc{
    [self.timer invalidate];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    NSLog(@"%f",scrollView.contentOffset.x);
    int pageindex=scrollView.contentOffset.x/XHScreenwidth;
    self.pagecontrol.currentPage=pageindex;
}
-(void)setPicarray:(NSArray *)picarray{
    _picarray =picarray;
    if (_picarray==nil) {
        _picarray=@[@"guanggao1",@"guanggao2"];
    }
    
    [self setupScroll];
}

-(void)setupScroll{
    for (int i=0; i<self.picarray.count-1; i++) {
        UIImageView *imageview=[[UIImageView alloc]init];
        imageview.contentMode=UIViewContentModeScaleAspectFill;
        imageview.height=self.scollview.height;
        imageview.y=0;
        imageview.x=i*XHScreenwidth;
        imageview.width=XHScreenwidth;
        imageview.clipsToBounds=YES;
        imageview.userInteractionEnabled=YES;
        imageview.tag=i;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageclick:)];
        [imageview addGestureRecognizer:tap];
        imageview.image=[UIImage imageNamed:self.picarray[i]];
        [self.scollview addSubview:imageview];
        
    }
    ScrollViewItem *item=[ScrollViewItem scrollitem];
    item.width=XHScreenwidth;
    item.x=XHScreenwidth;
    item.height=self.scollview.height;
    item.totalnumber.hidden=YES;
    [self.scollview addSubview:item];
    self.item=item;
    self.scollview.contentSize=CGSizeMake(XHScreenwidth*self.picarray.count, 0);
    self.scollview.delegate=self;
    self.scollview.showsHorizontalScrollIndicator=NO;
//    NSLog(@"%ld",self.scollview.subviews.count);
    self.pagecontrol.numberOfPages=_picarray.count;
}

-(void)imageclick:(UITapGestureRecognizer *)tap{
    UIImageView *image=tap.view;
    if ([_delegate respondsToSelector:@selector(HeaderImageClick:)]) {
        [_delegate HeaderImageClick:image.tag];
    }
}
-(void)setTotalnumber:(NSInteger)totalnumber{
    _totalnumber=totalnumber;
    self.item.totalnumber.hidden=NO;
    if (_totalnumber>10000) {
        self.item.totalnumber.text=[NSString stringWithFormat:@"%ld",_totalnumber];
        
    }else{
        self.item.totalnumber.text=[NSString stringWithFormat:@"00%ld",_totalnumber];
    }
}
-(IBAction)clickWith:(UIButton *)sender{
    if([_delegate respondsToSelector:@selector(click:)]) {
        [_delegate click:sender.tag];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
